//
//  LGCarePlanTaskOtherCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/9/19.
//  新增、编辑任务 -- 其他Cell

#import "LGCarePlanTaskOtherCell.h"

#define placeHolder  @"请输入..."

@interface LGCarePlanTaskOtherCell()

/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UITextView *contentTF;

@end

@implementation LGCarePlanTaskOtherCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.clipsToBounds = YES;
        self.backgroundColor = [UIColor clearColor];
        self.contentView.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:self.baseView];
        [self.baseView addSubview:self.contentTF];
        [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(viewPix(16));
            make.right.equalTo(self.contentView).offset(-viewPix(16));
            make.top.bottom.equalTo(self.contentView);
            make.height.equalTo(@(viewPix(90)));
        }];
    }
    return  self;
}

-(void)setContent:(NSString *)content{
    _content = content;
    if(content.length == 0 || [content isEqualToString:placeHolder]){
        self.contentTF.font = LGFont(14);
        self.contentTF.textColor = [UIColor colorWithString:@"#999999"];
        self.contentTF.attributedText = [placeHolder attributeString:@{NSFontAttributeName:LGFont(14),NSForegroundColorAttributeName:[UIColor colorWithString:@"#999999"]} alignment:NSTextAlignmentLeft lineSpacing:5];
    }else{
        self.contentTF.font = LGFont(15);
        self.contentTF.textColor = [UIColor colorWithString:@"#333333"];
        self.contentTF.attributedText = [content attributeString:@{NSFontAttributeName:LGFont(15),NSForegroundColorAttributeName:[UIColor colorWithString:@"#333333"]} alignment:NSTextAlignmentLeft lineSpacing:5];
    }
    CGFloat contentW = self.contentTF.bounds.size.width;
    CGFloat height = [self.contentTF.text textHeightWithFont:self.contentTF.font maxWidth:contentW lineSpacing:5];
//    [self.contentTF mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.height.equalTo(@(height));
//    }];
}

- (void)textViewDidBeginEditing:(UITextView *)textView{
    self.contentTF.textColor = [UIColor colorWithString:@"#333333"];
    if([textView.text isEqualToString:placeHolder]){
        self.contentTF.text = @"";
    }
}

-(void)textViewDidEndEditing:(UITextView *)textView{
    self.content = textView.text;
}

- (void)textViewDidChange:(UITextView *)textView{
    NSString *lang = textView.textInputMode.primaryLanguage; // 键盘输入模式
    NSString *content = textView.text;
    if ([lang isEqualToString:@"zh-Hans"]){
        UITextRange *selectedRange = [textView markedTextRange];
        if (!selectedRange) { // 没有高亮
            if (textView.text.length>30) { // 输入的文字大于设定的长度  这里设置的是200个字符
                textView.text = [textView.text substringToIndex:30];
            }else{
                textView.text = textView.text;
            }
            //将文字传出去
//            if(self.delegate && [self.delegate respondsToSelector:@selector(inputContentChanged:key:)]){
//                [self.delegate inputContentChanged:textView.text key:self.dataDic[@"key"]];
//            }
            
            if(self.inputContentChanged){
                self.inputContentChanged(textView.text);
            }
            
            if(textView.text.length>0){
                self.contentTF.attributedText = [textView.text attributeString:@{NSFontAttributeName:LGFont(15),NSForegroundColorAttributeName:[UIColor colorWithString:@"#333333"]} alignment:NSTextAlignmentLeft lineSpacing:5];
            }
//
//            CGSize maxSize = CGSizeMake(self.contentTF.size.width, MAXFLOAT);
//            CGSize fitSize = [textView sizeThatFits:maxSize];
//            CGFloat viewHeight = self.contentTF.bounds.size.height;
//            CGFloat newHeight = fitSize.height;
//            NSLog(@">>>???>>>%.2f--%.2f",newHeight,viewHeight);
//            [self.contentTF mas_updateConstraints:^(MASConstraintMaker *make) {
//                make.height.equalTo(@(newHeight));
//                UITableView *tableView = [self tableView];
//                [tableView beginUpdates];
//                [tableView endUpdates];
//            }];
//            if(newHeight != viewHeight){
//                [self.contentTF mas_updateConstraints:^(MASConstraintMaker *make) {
//                    make.height.equalTo(@(newHeight));
//                    UITableView *tableView = [self tableView];
//                    [tableView beginUpdates];
//                    [tableView endUpdates];
//                }];
//            }
        }
    }else{
        if (textView.text.length>30) {
            textView.text = [textView.text substringToIndex:30];
        }else{
            textView.text = textView.text;
        }
        //将文字传出去
//        if(self.delegate && [self.delegate respondsToSelector:@selector(inputContentChanged:key:)]){
//            [self.delegate inputContentChanged:textView.text key:self.dataDic[@"key"]];
//        }
        
        if(self.inputContentChanged){
            self.inputContentChanged(textView.text);
        }
        
        
        if(textView.text.length>0){
            self.contentTF.attributedText = [textView.text attributeString:@{NSFontAttributeName:LGFont(15),NSForegroundColorAttributeName:[UIColor colorWithString:@"#333333"]} alignment:NSTextAlignmentLeft lineSpacing:5];
        }
        
//        CGSize maxSize = CGSizeMake(Screen_W-viewPix(56), MAXFLOAT);
//        CGSize fitSize = [textView sizeThatFits:maxSize];
//        CGFloat viewHeight = self.contentTF.bounds.size.height;
//        CGFloat newHeight = fitSize.height;
//        NSLog(@">>>???>>>%.2f--%.2f",newHeight,viewHeight);
//        [self.contentTF mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.height.equalTo(@(newHeight));
//            UITableView *tableView = [self tableView];
//            [tableView beginUpdates];
//            [tableView endUpdates];
//        }];
//        if(newHeight != viewHeight){
//    //        NSLog(@">>>???>>>%.2f--%.2f",newHeight,viewHeight);
//
//        }
    }
}



-(UITableView *)tableView{
    UIView *tableView = self.superview;
    while (![tableView isKindOfClass:[UITableView class]] && tableView) {
        tableView = tableView.superview;
    }
    return (UITableView *)tableView;
}

#pragma mark
#pragma mark ====> 创建控件
-(UIView *)baseView{
    if(!_baseView){
        _baseView = [UIView shadowViewWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:0];
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}

-(UITextView *)contentTF{
    if(!_contentTF){
        _contentTF = [[UITextView alloc]initWithFrame:CGRectMake(viewPix(12), viewPix(10), Screen_W-viewPix(56), viewPix(80))];
        _contentTF.textContainerInset = UIEdgeInsetsMake(viewPix(8), viewPix(8), viewPix(8), viewPix(8));
        _contentTF.tintColor = [UIColor colorWithString:@"#333333"];
        _contentTF.textColor = [UIColor colorWithString:@"#999999"];
        _contentTF.backgroundColor = [UIColor colorWithString:@"#FAFAFA"];
        _contentTF.layer.borderColor = [UIColor colorWithString:@"#E3E3E3"].CGColor;
        _contentTF.layer.borderWidth = 0.5;
        _contentTF.cornerRidus = 5;
        _contentTF.textAlignment = NSTextAlignmentLeft;
        _contentTF.font = LGFont(14);
        _contentTF.delegate = self;
        _contentTF.scrollEnabled = NO;
    }
    return _contentTF;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
